package com.zhang.servlet.user;

import com.zhang.service.UserService;
import com.zhang.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author happy
 **/
@WebServlet("/studio/user/register.do")
public class RegisterDoServlet extends HttpServlet {
    // 由于表示过程中的对象，一般都是单例的，定义成属性，可以减少对象的创建，相对来说性能较好
    private final UserService userService = new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.读取信息
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //TODD: 输入合法性校验

        //2. 完成注册操作（密码加密+插入操作），放到service中处理
        //由于这个用户对象要放到session中,属于表现的对象,所以用view_object
        UserVO userVO = userService.register(username,password);
        // 3.注册之后直接登陆
        HttpSession session = req.getSession();
        session.setAttribute("currentUser",userVO);
        //4.重定向创作中心首页
        resp.sendRedirect("/studio/");
    }
}